package com.yonyougov.devops.cruxcisupport;


import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.PropertyFilter;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigInteger;

@Configuration
public class WebConfig {
    @Bean
    Jackson2ObjectMapperBuilderCustomizer cruxJackson2ObjectMapperBuilderCustomizer() {
        return b -> b.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).
                featuresToDisable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
                // 默认忽略 BaseEntity 上的 @JsonFilter("baseJsonFilter") 注解，否则调用 Feign 客户端，当参数为 BaseEntity
                // 子类时会报错，但是还需要解决转 json 时属性循环依赖导致死循环的问题，常规web请求时不会出现此问题，详见 FiltedResponseBodyAdvice 类
                // 蒲强华 解决 Entity 转 json 时循环依赖死循环的问题
                .filters(new FilterProvider() {
                    @SuppressWarnings("deprecation")
                    @Override
                    public com.fasterxml.jackson.databind.ser.BeanPropertyFilter findFilter(Object filterId) {
                        return null;
                    }

                    @Override
                    public PropertyFilter findPropertyFilter(Object filterId, Object valueToFilter) {
                        return null;
                    }
                }).serializerByType(Long.class, ToStringSerializer.instance).serializerByType(BigInteger.class, ToStringSerializer.instance);
    }
}

